package cate.game.gm.sub;

import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.game.GameBody;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.Role;
import cate.game.role.bag.IBagContainer;
import cate.game.role.bag.RoleBag;
import cate.game.role.bag.item.BaseBagItem;
import cp.solution.business.game.bag.BagDeductCmd;
import cp.solution.business.game.bag.BagDeductResult;
import cp.solution.business.game.bag.BagItemView;
import cp.solution.business.game.bag.BagView;
import easy.java.practice.errorcode.EcException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@Slf4j
public class BagForGm {

    public static BagView doView(GameBody game, String roleId) {
        log.info("收到查看角色背包信息的请求 roleId={}", roleId);
        Role role = game.role.getRole(roleId);
        if(role == null) {
            throw new EcException("角色不存在("+roleId+")");
        }
        PlayBuildContext pbc = game.fight.getPlayBuildContext();
        BagView bag = new BagView();
        RoleBag rb = role.getBag();
        handleSubView(pbc, bag.numRes, rb.numRes);
        handleSubView(pbc, bag.hero, rb.hero);
        handleSubView(pbc, bag.equip, rb.getEquip());
        handleSubView(pbc, bag.fairy, rb.getFairy());
        handleSubView(pbc, bag.heroPart, rb.heroPart);
        handleSubView(pbc, bag.prop, rb.prop);
        handleSubView(pbc, bag.rune, rb.rune);
        handleSubView(pbc, bag.furniture, rb.furniture);
        handleSubView(pbc, bag.spice, rb.spice);
        handleSubView(pbc, bag.gem, rb.gem);
        handleSubView(pbc, bag.inscription, rb.inscription);
        return bag;
    }

    private static void handleSubView(PlayBuildContext ctx, List<BagItemView> subItems, IBagContainer<? extends BaseBagItem> container) {
        try {
            for(BaseBagItem ri : container.getList()) {
                BagItemView view = new BagItemView();
                view.uid = ri.uid;
                view.type = ri.type;
                view.tid = ri.tid;
                view.num = ri.num;
                view.name = ri.toName(ctx);
                view.detail = ri.toDetail();
                subItems.add(view);
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }

    public static BagDeductResult doDeduct(GameBody game, BagDeductCmd cmd) {
        BagDeductResult result = new BagDeductResult();
        result.read(cmd);
        Role role = game.role.getRole(cmd.roleId);
        if(role == null) {
            throw new EcException("角色不存在("+cmd.roleId+")");
        }
        result.roleName = role.getBase().name;
        IBagContainer<?> itsBag = role.getBag().getBag(cmd.itemType);
        if(itsBag == null) {
            throw new EcException("背包类型错误("+cmd.itemType +")");
        }
        if(StringUtils.equals(cmd.way, BagDeductCmd.WAY_BY_UID)) {
            BaseBagItem item = itsBag.getItem(cmd.itemUid);
            if(item == null) {
                throw new EcException("找不到item, 该物品可能已经被移除了");
            }
            result.itemName = item.toName(role.toPlayBuildContext());
            result.itemNumOld = itsBag.count(item.tid);
            role.log.setOperation(GDOperation.GM_BAG_DEDUCT);
            itsBag.removeItemByUid(cmd.itemUid, Math.abs(cmd.changeNum), null);
            role.log.clearOperation();
            result.itemNumNew = itsBag.count(item.tid);
        } else if(StringUtils.equals(cmd.way, BagDeductCmd.WAY_BY_TID)) {
            if(cmd.itemType == GDObj.Type.NUM) {
                result.itemName = GDObj.Type.name(cmd.itemType) + ":" + GDObj.Num.name((byte)cmd.itemTid);
            } else {
                result.itemName = GDObj.Type.name(cmd.itemType) + ":" + cmd.itemTid;
            }
            result.itemNumOld = itsBag.count(cmd.itemTid);
            role.log.setOperation(GDOperation.GM_BAG_DEDUCT);
            itsBag.removeItem(cmd.itemTid, Math.abs(cmd.changeNum), null);
            role.log.clearOperation();
            result.itemNumNew = itsBag.count(cmd.itemTid);
        } else {
            throw new EcException("不支持的way参数:"+cmd.way);
        }
        return result;
    }
}
